let card=function(){
  let hasInitIdCard=false;
  function init(containerId){
    if(!hasInitIdCard)
    {
      document.getElementById(containerId).innerHTML="<OBJECT classid='clsid:F1317711-6BDE-4658-ABAA-39E31D3704D3' codebase='SDRdCard.cab#version=1,3,6,1' width='0' height='0' align='center' hspace='0' vspace='0' id='rdcard' name='rdcard'></OBJECT>";
      hasInitIdCard=true;
    }
  }

  function uninit(containerId){
    document.getElementById(containerId).innerHTML="";
    hasInitIdCard=false;
  }
  return {
    idCard: {
      load:function(containerId){
        init(containerId);
        return this;
      },
      unload:function(containerId){
        uninit(containerId);
        return this;
      },
      readIdCard: function (filepath) {
        rdcard.OpenPort();
        let returnValue = rdcard.readcard();
        if (returnValue != 0) {
          alert("身份证读取失败，请检查！");
          return;
        }

        let resMsg = {
          imgpath1: rdcard.JPGBuffer,
          imgpath2: "",
          imgpath3: "",
          name: rdcard.NameL, // 姓名
          sex: rdcard.SexL,  // 性别
          nation: rdcard.NationL,  // 民族
          birthDate:  rdcard.Born.substr(0, 4) + "-" + rdcard.Born.substr(4, 2) + "-" + rdcard.Born.substr(6, 2), // 出生日期
          idCardNo: rdcard.CardNo, // 身份证号码
          organization: rdcard.Police, // 签发机关
          address:rdcard.Address, // 地址
          validityCertificateDate: rdcard.ActivityFrom.substr(0, 4) + "-" + rdcard.ActivityFrom.substr(4, 2) + "-" + rdcard.ActivityFrom.substr(6, 2)+"至"+rdcard.ActivityTo.substr(0, 4) + "-" + rdcard.ActivityTo.substr(4, 2) + "-" + rdcard.ActivityTo.substr(6, 2) // 证件有效期
        };
        console.log(resMsg);
        return resMsg;
      }
    }
  }
}();

export default card;
